package com.maxsalad.uc.dao;

import com.maxsalad.common.dao.BaseDao;
import com.maxsalad.uc.dto.DOrderDto;
import com.maxsalad.uc.pojo.TOrderEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * Created by 2014-400 on 2016-03-17.
 */
@Repository
public class OrderDao extends BaseDao<TOrderEntity> {
    private void baseSearchParam(DOrderDto orderDto, StringBuffer hql) {
        if (orderDto == null) {
            return;
        }
        if (orderDto.getId() != 0) {
            hql.append(" and t.id =:id");
        }
        //shopId
        if (orderDto.getShopId() != 0) {
            hql.append(" and t.shopId =:shopId");
        }
        //status
        if (StringUtils.isNotBlank(orderDto.getStatus())) {
            hql.append(" and t.status =:status");
        }
        //联系人
        if (StringUtils.isNotBlank(orderDto.getContactName())) {
            hql.append(" and t.contactName =:contactName");
        }
        //联系电话
        if (StringUtils.isNotEmpty(orderDto.getContactTel())) {
            hql.append(" and t.contactTel =:contactTel");
        }
        //联系地址
        if (StringUtils.isNotEmpty(orderDto.getContactAddress())) {
            hql.append(" and t.contactAddress like :contactAddress");
            orderDto.setContactAddress("%" + orderDto.getContactAddress() + "%");
        }
        //订单开始时间
        if (orderDto.getStartOrderTime() != 0) {
            hql.append(" and t.orderTime >=:startOrderTime");
        }
        //订单结束时间
        if (orderDto.getEndOrderTime() != 0) {
            hql.append(" and t.orderTime <=:endOrderTime");
        }

    }

    /**
     * 查询所有平台信息
     *
     * @param orderDto
     * @return
     * @throws Exception
     */
    public Map queryAllOrder(DOrderDto orderDto) throws Exception {
        StringBuffer hql = new StringBuffer("from TOrderEntity t where 1=1 ");
        this.baseSearchParam(orderDto, hql);
        hql.append(" order by t.createTime desc ");
        return this.pageQueryByHql(hql.toString(), orderDto);
    }

    /**
     * 不分页查询
     *
     * @param orderDto
     * @return
     * @throws Exception
     */
    public List<TOrderEntity> queryAllOrderNoPage(DOrderDto orderDto) throws Exception {
        StringBuffer hql = new StringBuffer("from TOrderEntity t where 1=1 ");
        this.baseSearchParam(orderDto, hql);
        hql.append(" order by t.createTime desc ");
        return this.findByHql(hql.toString(), orderDto);
    }


    public List<TOrderEntity> queryOrder(long shopId, String status, int size) throws Exception {
        DOrderDto orderDto = new DOrderDto();
        orderDto.setShopId(shopId);
        orderDto.setStatus(status);
        orderDto.setPageSize(size);

        StringBuffer hql = new StringBuffer("from TOrderEntity t where 1=1 ");
        this.baseSearchParam(orderDto, hql);
        hql.append(" order by t.createTime desc ");
        return this.pageQuery(hql.toString(), orderDto, false);
    }


    public TOrderEntity updateNewId(long id, String status, String newId) throws Exception {
        TOrderEntity entity = this.get(id);
        entity.setStatus(status);
        entity.setNewId(newId);

        return this.saveOrUpdate(entity);
    }

}
